#include "WProgram.h"
#include "Sound.h"

void Sound::refreshStatus(){
  //next step of current melody
  if(!isPlaying() && playing){
    //there is no sound playing in the buzzer but there is a song to play
    //start playing next note
    if(noteBuffer[nCurrentNote].duration > 0){
      //there is a next note to play
      play(noteBuffer[nCurrentNote].pitch,noteBuffer[nCurrentNote].duration);
      
      Serial.print("pitch: ");
      Serial.print(noteBuffer[nCurrentNote].pitch);
      Serial.print("duration: ");
      Serial.print(noteBuffer[nCurrentNote].duration);
      Serial.println("------");
      
		nCurrentNote++;
    }
    else{
      //the melody is over reset the status
      playing = false;
      nCurrentNote = 0;
      noteBufferMax = 0;
      noteBuffer[0].duration = 0;
    }
  }
}

void Sound::setMelody(Note & melody,int nNotes){
   
  int offset = 0;
  
  while(offset < nNotes){
    Note note = *(&melody+offset);
    noteBuffer[noteBufferMax] = note;
    offset++;
    noteBufferMax++;    
  }
  if(!playing){
    nCurrentNote = 0;
    playing = true;
  }
}

void Sound::shoot(){
 Note melody[] = {NOTE_A4,70,NOTE_A2,30};
 int nNotes = sizeof(melody)/sizeof(melody[0]);
 setMelody(*melody,nNotes);
}

void Sound::respawning(){
}

void Sound::playerReady(){
 Note melody[] = {{NOTE_A4,200},{NOTE_G2,90},{NOTE_G2,90},{NOTE_A4,200}};
 int nNotes = sizeof(melody)/sizeof(melody[0]);
 setMelody(*melody,nNotes);
}

void Sound::death(){
}

void Sound::hit(){
 Note melody[] = {NOTE_A2,70,NOTE_A4,130};
 int nNotes = sizeof(melody)/sizeof(melody[0]);
 setMelody(*melody,nNotes);
}

void Sound::gameOver(){
}

void Sound::respawnStart(){
}

void Sound::outOfAmmo(){
}

void Sound::nearMiss(){
}

void Sound::startedReloading(){
}

void Sound::finishedReloading(){
}
